package com.jz.community.moduleshoppingguide.neighbor.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Andy Huang on 2018/4/27.
 *
 * @description:
 */
public class TimeUtile {
    private static final long ONEDATE = 86400000; // 一天的时间差
    private static final long ONEHOUR = 3600000; // 一小时的时间差
    private static final long ONEMINUTE = 60000; // 一分钟的时间差
    private static final long ONEMIAO = 1000; // 一分钟的时间差
    /**
     * time类型 是 yyyy-MM-dd HH:mm:ss.s 不是的请转化成这个
     * <p>
     * time 未与当前时间相比的时间  time  就是未来的时间
     *
     * @param time
     * @return
     */

    public static String putDate(String time) {
        String dateStr = null;
        try {
//          SimpleDateFormat dd = new SimpleDateFormat("dd");
            SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date otheTime = yyyyMMdd.parse(time);
            Date nowTime = new Date();
//          Date date10 = yyyyMMdd.parse("2015-10-10 10:30:30.0");
//          Date date11 = yyyyMMdd.parse("2015-10-10 23:40:30.0");
            Long time10 = nowTime.getTime();
            Long time11 = otheTime.getTime();
//          System.out.println("time===  " + time10 + "  time11 " + time11 + "   time10差值" + (time11 - time10));
//          System.out.println("用了多久===  " + (time11 - time10) / ONEDATE);
//          System.out.println("====" + dd.format(new Date(time11 - time10)));
            // dateStr = yyyyMMdd.format(new Date(time10));
            long timeDiff = time10 - time11;

            if (timeDiff >= ONEDATE) {
                String someDate = someDate(timeDiff, time);
                return someDate == null ? time : someDate;
            }
            if (timeDiff >= ONEHOUR) {
                return someHour(timeDiff, time);
            }
            if (timeDiff >= ONEMINUTE) {
                return someMinute(timeDiff);
            }
            if (timeDiff <= ONEMIAO) {
                return someMinute(timeDiff);
            }

        } catch (ParseException e) {

            e.printStackTrace();
        }

        return dateStr;
    }

    private static String someMinute(long timeDiff) {
        int dateNum = (int) (timeDiff / ONEMINUTE);
        System.out.println("dateNum  minute==  " + dateNum);
        String dateStr;
        if (dateNum >= 0 && dateNum < 1) {
            dateStr = "刚刚";
        } else if (dateNum < 60) {
            dateStr = dateNum + "分钟前";
        } else {
            dateStr = "1个小时前";
        }

        return dateStr;

    }

    /**
     * 判断相差小时
     *
     * @param timeDiff
     * @return
     */

    private static String someHour(long timeDiff, String time) {
        int dateNum = (int) (timeDiff / ONEHOUR);
        System.out.println("dateNum  someHour==  " + dateNum);
        String dateStr;
        if (dateNum < 12) {
//            dateStr = getDate(dateNum, "小时前");
            dateStr = String.valueOf(dateNum) + "小时前";
        } else if (dateNum >= 12 && dateNum <= 24) {
            dateStr = String.valueOf(dateNum) + "小时前";
        } else {
            dateStr = time;
        }
        return dateStr;
    }

    /**
     * 用于判断相差月数
     *
     * @param timeDiff
     * @return
     */
    private static String someDate(long timeDiff, String time) {
        int dateNum = (int) (timeDiff / ONEDATE);
        System.out.println("dateNum  someDate==  " + dateNum);
        String dateStr = time;
//        if (dateNum <= 15) {
//            dateStr = getDate(dateNum, "天前内");
//        } else if (dateNum <= 30 && dateNum > 15) {
//            dateStr = "一个月内";
//        } else if (dateNum < 60) {
//            dateStr = "两个月内";
//        } else {
//            dateStr = "很久很久以前";
//        }
        return dateStr;
    }

    /**
     * 组装提示
     *
     * @param index
     * @param str
     * @return
     */
    private static String getDate(int index, String str) {
        return String.format("%s%s", enNum2CnNum(String.valueOf(index)), str);
    }

    /**
     * 数字转化成中国的一二三
     *
     * @param num
     * @return
     */
    public static String enNum2CnNum(String num) {
        String[] CnNum = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        //自己最大值 只需要 在这里添加就行了 其他代码不用改 例如到 亿
        String[] Cn10Num = new String[]{"十", "百", "千", "万"};
        String CnStr = "";

        try {
            char[] strArray = num.toCharArray();
            int length = strArray.length;
            for (int i = 0; i < strArray.length; i++) {
                if (length > 1 && ((char2Int(strArray[i])) != 0)) {
                    CnStr += (CnNum[char2Int(strArray[i])] + Cn10Num[length - 2]);
                } else {
                    CnStr += CnNum[char2Int(strArray[i])];
                }
                length--;
            }
            // 处理多个零字段
            CnStr = CnStr.replaceAll("零零*", "零");
            // 处理最后一个是零
            if (CnStr.lastIndexOf("零") + 1 == CnStr.length()) {
                CnStr = CnStr.replaceAll("零$", "");
            }
            // 处理十几
            if (CnStr.contains("十") && (CnStr.length() == 3 || CnStr.length() == 2)) {
                CnStr = CnStr.replaceAll("^一", "");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return CnStr;

    }

    /**
     * 获取单个的String
     *
     * @param cha
     * @return
     */
    private static Integer char2Int(char cha) {
        return Integer.valueOf(cha + "");
    }

    public static void main(String[] args) {
//        System.out.println("====  " + putDate("2015-12-30 10:20:30.0"));
//      System.out.println(enNum2CnNum("10000"));

    }
}
